import sys
import jieba
from random import random
from elasticsearch import Elasticsearch, helpers


import requests
import json


def get_vector(text):
    try:
        url = 'http://172.18.8.118:8120/faq/get_vector?data=%s' % ([text])
        result = requests.get(url).text
        res = json.loads(result)
        return res['data']['result'][0]['question_vector']
    except Exception as e:
        print("error: ", e)
        return str([0] * 300)

es_client = Elasticsearch(hosts=['172.18.8.118:9200', '172.18.8.118:9201'])
query = {'query': {'match_all': {}}}

page_num = 1000  # 每次获取数据

query = es_client.search(index="test_faq", body=query, scroll='8m', size=page_num)

results = query['hits']['hits']  # es查询出的结果第一页
total = query['hits']['total']  # es查询出的结果总量
scroll_id = query['_scroll_id']  # 游标用于输出es查询出的所有结果
every_num = int(total['value']/page_num)  #

alist = []
for i in range(0, every_num+1):
    # scroll参数必须指定否则会报错
    query_scroll = es_client.scroll(scroll_id=scroll_id, scroll='8m')['hits']['hits']
    results += query_scroll


# print("size:", len(res['hits']['hits']))
for i, hit in enumerate(results):
    question = hit["_source"]["question"]
    print(i, question)
    a = get_vector(question)
    a = eval(a)
    es_client.update(index='test_faq', doc_type='_doc', id=hit['_id'], body={"doc":{"question_vector": a}})
